<?php

namespace Sharks\Core\Support\Log;

use Sharks\Core\AppContext;
use Sharks\Core\Cfg\AppConfig;
use Sharks\Core\Support\Base\DefValue;
use Sharks\Core\Support\Base\Precondition;

/**
 * 日志
 */
abstract class LogFactory {

    const DEBUG = 100;
    const INFO = 200;
    const WARNING = 300;
    const ERROR = 400;
    const CRITICAL = 500;
    const ALTER = 600;

    abstract function debug($msg);
    abstract function info($msg);
    abstract function warning($msg);
    abstract function error($msg);
    abstract function critical($msg);
    abstract function alter($msg);

    public function isDebug() {
        $level = strtoupper(AppConfig::get("logging.state"));
        return $this->getLevelNumber($level) <= self::DEBUG ? true : false;
    }

    public function isInfo() {
        $level = strtoupper(AppConfig::get("logging.state"));
        return $this->getLevelNumber($level) <= self::INFO ? true : false;
    }

    public function isWarning() {
        $level = strtoupper(AppConfig::get("logging.state"));
        return $this->getLevelNumber($level) <= self::WARNING ? true : false;
    }

    public function isError() {
        $level = strtoupper(AppConfig::get("logging.state"));
        return $this->getLevelNumber($level) <= self::ERROR ? true : false;
    }

    public function isCritical() {
        $level = strtoupper(AppConfig::get("logging.state"));
        return $this->getLevelNumber($level) <= self::CRITICAL ? true : false;
    }

    public function isAlter() {
        $level = strtoupper(AppConfig::get("logging.state"));
        return $this->getLevelNumber($level) <= self::ALTER ? true : false;
    }

    protected function getLevelNumber($level) {
        $level = strtoupper($level);
        if ("DEBUG" == $level) {
            return self::DEBUG;
        }
        elseif ("INFO" == $level) {
            return self::INFO;
        }
        elseif ("WARNING" == $level) {
            return self::WARNING;
        }
        elseif ("ERROR" == $level) {
            return self::ERROR;
        }
        elseif ("CRITICAL" == $level) {
            return self::CRITICAL;
        }
        elseif ("ALTER" == $level) {
            return self::ALTER;
        }
        return 10000;
    }

}